import 'package:flutter/material.dart';

import 'calculate_ema.dart';

class MACDChart extends StatelessWidget {
  final List<double> closingPrices;

  MACDChart({required this.closingPrices});

  @override
  Widget build(BuildContext context) {
    // 计算 MACD 数据
    // List<List<double>> macdData = calculateMACD(closingPrices);
    // List<double> macdLine = macdData[0];
    // List<double> signalLine = macdData[1];
    // List<double> histogram = macdData[2];

    // 示例价格数据
    List<double> closingPrices = [
      23.0,
      24.0,
      25.0,
      26.0,
      27.0,
      26.5,
      29.0,
      30.0,
      32.0,
      33.5,
      34.0,
      35.0,
      34.5,
      37.0,
      36.0,
      38.0,
      39.5,
      40.0,
      42.0,
      43.0,
      45.0,
      46.0,
      47.5,
      48.5,
      50.0,
      51.0,
      52.0,
      53.0,
      54.0,
      56.0,
      58.0,
      60.0,
      61.5,
      62.5,
      64.0,
      65.0,
      66.0,
      67.0,
      68.0
    ];

    // 计算 MACD
    List<double> macdLine = calculateMACD(closingPrices);
    List<double> signalLine = calculateSignalLine(macdLine);
    List<double> histogram = calculateHistogram(macdLine, signalLine);

    // 输出结果
    for (int i = 0; i < macdLine.length; i++) {
      print("macdLine[$i]: ${macdLine[i]}");
      if (i < signalLine.length) {
        print("signalLine[$i]: ${signalLine[i]}");
      }
      if (i < histogram.length) {
        print("histogram[$i]: ${histogram[i]}");
      }
    }

    return CustomPaint(
      size: const Size(double.infinity, 400),
      painter: MACDPainter(macdLine, signalLine, histogram),
    );
  }
}

class MACDPainter extends CustomPainter {
  final List<double> macdLine;
  final List<double> signalLine;
  final List<double> histogram;

  MACDPainter(this.macdLine, this.signalLine, this.histogram);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.5;

    // 绘制 MACD 线
    paint.color = Colors.blue;
    for (int i = 1; i < macdLine.length; i++) {
      double startX = (i - 1) * (size.width / macdLine.length);
      double startY = size.height / 2 - macdLine[i - 1] * 50;
      double endX = i * (size.width / macdLine.length);
      double endY = size.height / 2 - macdLine[i] * 50;

      canvas.drawLine(Offset(startX, startY), Offset(endX, endY), paint);
    }

    // 绘制信号线
    paint.color = Colors.red;
    for (int i = 1; i < signalLine.length; i++) {
      double startX = (i - 1) * (size.width / signalLine.length);
      double startY = size.height / 2 - signalLine[i - 1] * 50;
      double endX = i * (size.width / signalLine.length);
      double endY = size.height / 2 - signalLine[i] * 50;

      canvas.drawLine(Offset(startX, startY), Offset(endX, endY), paint);
    }

    // 绘制柱状图
    for (int i = 0; i < histogram.length; i++) {
      double startX = i * (size.width / histogram.length);
      double endX = (i + 1) * (size.width / histogram.length);
      double height = histogram[i] * 50;
      paint.color = histogram[i] >= 0 ? Colors.green : Colors.black;

      canvas.drawRect(
        Rect.fromLTRB(startX, size.height / 2, endX, size.height / 2 - height),
        paint,
      );
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
